class code2 {
    //方法二：bfs
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        //空数
        if(root == null) return ret;

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()) {
            int sz = q.size();
            for(int i=0;i<sz;i++) {
                TreeNode x = q.poll();
                if(x.left!=null)
                    q.offer(x.left);
                if(x.right!=null)
                    q.offer(x.right);
                if(i == sz-1) {
                    //这层的最后一个
                    ret.add(x.val);
                }
            }
        }
        return ret;
    }
}